﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Controllers;

namespace Web
{
    public partial class IMProfileEdit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
                var list = ctx.CMS_IMGrade.OrderBy(r => r.OrderNo);
                IMGradeId.DataSource = list;
                IMGradeId.DataTextField = "Grade";
                IMGradeId.DataValueField = "Id";
                IMGradeId.DataBind();
                IMGradeId.Items.Insert(0, new ListItem("", ""));

                if (Request.Params["Id"] != null)
                {
                    int id = int.Parse(Request.Params["Id"]);

                    CMS_Intermediation model = ctx.CMS_Intermediations.Single(q => q.Id == id);
                    IMGradeId.Value = model.IMGradeId.ToString();
                    IMNO.Value = model.IMNO;
                    Name.Value = model.Name;
                    Sex.Value = model.Sex;
                    EduLevel.Value = model.EduLevel;
                    GraduateFrom.Value = model.GraduateFrom;
                    IDNO.Value = model.IDNO;
                    Address.Value = model.Address;
                    Tel.Value = model.Tel;
                    EMail.Value = model.EMail;
                    UrgentContacter.Value = model.UrgentContacter;
                    UrgentContacterTel.Value = model.UrgentContacterTel;
                    IsValid.Value = model.IsValid?"1":"0";
                }
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
            CMS_Intermediation model;
            string id = Request.QueryString["id"];
            if (!string.IsNullOrEmpty(id))
            {
                model = ctx.CMS_Intermediations.Single(r => r.Id == int.Parse(id));
            }
            else
            {
                model = new CMS_Intermediation();
                ctx.CMS_Intermediations.InsertOnSubmit(model);
            }
            model.IMGradeId = int.Parse(IMGradeId.Value);
            model.IMNO = IMNO.Value;
            model.Password ="123456";
            model.Name = Name.Value;
            model.Sex = Sex.Value;
            model.EduLevel = EduLevel.Value;
            model.GraduateFrom = GraduateFrom.Value;
            model.IDNO = IDNO.Value;
            model.Address = Address.Value;
            model.Tel = Tel.Value;
            model.EMail = EMail.Value;
            model.UrgentContacter = UrgentContacter.Value;
            model.UrgentContacterTel = UrgentContacterTel.Value;
            model.IsValid = IsValid.Value=="1";
            ctx.SubmitChanges();
            Response.Write("<script>window.returnValue='Succeed';window.close();</script>");
        }
    }
}